package com.smartscreen.grademanage.service.impl;


import com.smartscreen.grademanage.dto.GenerateReportRequest;
import com.smartscreen.grademanage.dto.GenerateReportResponse;
import com.smartscreen.grademanage.dto.StudentInfoDTO;
import com.smartscreen.grademanage.entity.Exam;
import com.smartscreen.grademanage.entity.LearningReport;
import com.smartscreen.grademanage.mapper.ExamMapper;
import com.smartscreen.grademanage.mapper.LearningReportMapper;
import com.smartscreen.grademanage.service.LearningReportService;
import com.smartscreen.grademanage.service.StudentInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.UUID;

@Service
public class LearningReportServiceImpl implements LearningReportService {
    
    @Autowired
    private LearningReportMapper learningReportMapper;
    
    @Autowired
    private ExamMapper examMapper;
    
    @Autowired
    private StudentInfoService studentInfoService;
    
    @Override
    public GenerateReportResponse generateReport(String studentId, GenerateReportRequest request) {
        String reportId = "report_" + UUID.randomUUID().toString().replace("-", "");
        
        // 使用MyBatis Plus查询学生信息
        StudentInfoDTO studentInfo = studentInfoService.getStudentById(studentId);
        String studentName = studentInfo != null ? studentInfo.getName() : "未知学生";
        
        // 使用MyBatis Plus查询考试信息
        Exam exam = examMapper.selectById(request.getExamId());
        String examName = exam != null ? exam.getExamName() : "未知考试";
        
        LearningReport report = new LearningReport();
        report.setId(reportId);
        report.setStudentId(studentId);
        report.setExamId(request.getExamId());
        report.setStudentName(studentName);
        report.setExamName(examName);
        report.setStatus("GENERATING");
        
        learningReportMapper.insert(report);
        
        // 这里应该异步调用AI服务生成报告
        // 模拟异步处理
        
        GenerateReportResponse response = new GenerateReportResponse();
        response.setReportId(reportId);
        response.setStatus("GENERATING");
        
        return response;
    }
    
    @Override
    public LearningReport getReport(String reportId) {
        return learningReportMapper.selectById(reportId);
    }
}